﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.StaticFactory;

namespace UnityDemo
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			var container = new UnityContainer();
			container.AddExtension(new FactoryDefaultLifetime());
			var staticConfig = container.AddNewExtension<StaticFactoryExtension>()
				.Configure<IStaticFactoryConfiguration>();

			staticConfig.RegisterFactory<IFoo>(c => new Foo());

			var foo = container.Resolve<IFoo>();
			var foo2 = container.Resolve<IFoo>();

			MessageBox.Show(Object.ReferenceEquals(foo, foo2).ToString());
		}
	}

	public interface IFoo { }
	public class Foo : IFoo { }

	class FactoryDefaultLifetime : Microsoft.Practices.Unity.UnityContainerExtension
	{
		protected override void Initialize()
		{
			Context.Policies.SetDefault<Microsoft.Practices.ObjectBuilder2.ILifetimePolicy>(
				new Microsoft.Practices.ObjectBuilder2.TransientLifetimePolicy());
		}
	}
}